package com.example.service;

import com.example.dto.LoginResponse;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.utils.JwtUtils;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public LoginResponse login(String username, String password) {
        User user = userMapper.selectByUsername(username);
        if (user == null || !user.getPasswordHash().equals(password)) {
            return null;
        }

        // 生成token
        String token = JwtUtils.generateToken(username, user.getUserId());

        // 构建响应
        LoginResponse response = new LoginResponse();
        response.setId(user.getUserId());
        response.setUsername(user.getUsername());
        response.setIsAdmin(user.getIsAdmin());
        response.setToken(token);

        return response;
    }
} 